package com.tuantuan.common.base.result;

import com.tuantuan.common.base.common.IResponseErrorCode;

/**
 * @author wangqinmin
 * @date 2018-11-08
 * @description: 仰天大笑出门去，我辈岂是蓬蒿人
 */
public enum BusinessCode implements IResponseErrorCode {

    /**
     * 失败
     */
    FAILED(0, "操作失败"),
    /**
     * 成功
     */
    SUCCESS(1, "执行成功"),

    /**
     * 警告
     */
    WARNING(-1, "执行成功"),
    /**
     * 你请求的路径不存在
     */
    NOT_FOUND(404, "你请求的路径不存在"),

    /**
     * 请求参数校验异常
     */
    PARAMETER_EXCEPTION(5001, "请求参数校验异常"),

    /**
     * 系统异常
     */
    SYSTEM_EXCEPTION(5000, "系统异常!"),

    /**
     * 请求参数解析异常
     */
    PARAMETER_PARSE_EXCEPTION(5002, "请求参数解析异常"),

    /**
     * HTTP Media 类型异常
     */
    HTTP_MEDIA_TYPE_EXCEPTION(5003, "HTTP Media 类型异常"),

    /**
     * 对象拷贝失败
     */
    BEAN_COPY_FAILED(1003, "对象拷贝失败");

    private final long code;
    private final String msg;

    BusinessCode(long code, final String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static BusinessCode fromCode(long code) {
        BusinessCode[] ecs = BusinessCode.values();
        for (BusinessCode ec : ecs) {
            if (ec.getCode() == code) {
                return ec;
            }
        }
        return SUCCESS;
    }

    @Override
    public long getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }

    @Override
    public String toString() {
        return String.format(" ErrorCode:{code=%s, msg=%s} ", code, msg);
    }
}
